﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml.Linq;
using Acando.Dynamics.CRM.Silverlight.Library.CrmSdk;
using System.Collections.ObjectModel;
using Acando.Dynamics.CRM.Silverlight.Library.Helper;
using Acando.Dynamics.CRM.Silverlight.Library.Dialogs;
using Acando.Dynamics.CRM.Silverlight.Library.Interfaces;
using Acando.Dynamics.CRM.Silverlight.Library.MetaSdk;

namespace Acando.Dynamics.CRM.Silverlight.Library.Controls.FieldControls
{
    /// <summary>
    /// Interaction logic for CrmLookupControl.xaml
    /// </summary>
    public partial class CrmLookupControl : BaseValueControl
    {

        private IMetaDataLayer _metadatalayer;
        #region Properties

        private LookupDialog _lockupDialog;

        public CrmReference Value
        {
            get { return (CrmReference)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for LookupId.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(CrmReference), typeof(CrmLookupControl), null);

        

        private LookupAttributeMetadata LookupMeta { get; set; }

        private IRecordControl MainControl { get; set; }

        #endregion

        #region Constructors

        public CrmLookupControl(IRecordControl recordControl, string controlType, string fieldName, 
                                    LookupAttributeMetadata lookupmeta, IMetaDataLayer metadatalayer)
            : base(recordControl, controlType, fieldName)
        {
            InitializeComponent();

            this.LookupMeta = lookupmeta;
            this.MainControl = recordControl;
            this._metadatalayer = metadatalayer;
            this.SetBinding(CrmLookupControl.ValueProperty, base.GetBinding(recordControl,controlType,fieldName));
            this.lblValueName.SetBinding(TextBox.TextProperty, base.GetBinding(recordControl,controlType+"display",fieldName));

            this.btnShowDialog.IsEnabled = !recordControl.IsReadOnly;
        }

        #endregion
         
        #region Events

        private void btnShowDialog_Click(object sender, RoutedEventArgs e)
        {
            if (_lockupDialog == null)
            {
                
                this._lockupDialog = new LookupDialog(this.LookupMeta, MainControl.Organame, this._metadatalayer);
                _lockupDialog.DialogClosed += new EventHandler<EventArgs>(dialog_Closed);
                _lockupDialog.ShowDialog();
            }
            else
                _lockupDialog.ShowDialog();
        }

        void dialog_Closed(object sender, EventArgs e)
        {
            if ((sender as LookupDialog).DialogResult)
                this.Value = ((LookupDialog)sender).ResultValue;
        }       

        #endregion
    }
}
